<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseleave="isShowCategoryList = false">
        <h2 class="all" @mouseenter="isShowCategoryList = true">分类</h2>
        <div
          class="sort"
          v-if="$route.path === '/shouye' || isShowCategoryList"
        >
          <div class="all-sort-list2" @click="toSearch">
            <div class="item" v-for="c1 in categoryList" :key="c1.categoryId">
              <!-- 一级分类 -->
              <h3>
                <a
                  :data-id="c1.categoryId"
                  :data-name="c1.categoryName"
                  :data-level="1"
                  >{{ c1.categoryName }}</a
                >
              </h3>

              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    class="fore"
                    v-for="c2 in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <!-- 二级分类 -->
                    <dt>
                      <a
                        :data-id="c2.categoryId"
                        :data-name="c2.categoryName"
                        :data-level="2"
                        >{{ c2.categoryName }}</a
                      >
                    </dt>

                    <dd>
                      <!-- 三级分类 -->
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                        <a
                          :data-id="c3.categoryId"
                          :data-name="c3.categoryName"
                          :data-level="3"
                          >{{ c3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <nav class="nav">
        <a href="###" class="nav-first">
          <img src="./images/161185054090483911.png" alt="大聚惠" />
        </a>

        <a href="###">苏宁家电</a>
        <a href="###">手机数码</a>
        <a href="###">苏宁超市</a>
        <a href="###">家具建材</a>
        <a href="###">生活家电</a>
        <a href="###">帮客服务</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { reqGetCategoryList } from "@/api/home";
export default {
  name: "TypeNav",
  data() {
    return {
      isShowCategoryList: false,
      categoryList: [],
    };
  },
  // 发送请求分类数据
  async mounted() {
    const result = await reqGetCategoryList();
    // console.log(result);
    this.categoryList = result;
  },
  methods: {
    toSearch(e) {
      const { id, name, level } = e.target.dataset;
      if (!id) return;
      this.$router.push({
        name: "Search",
        query: {
          ...this.$route.query,
          [`category${level}Id`]: id,
          categoryName: name,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #ff8000;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      display: flex;
      a {
        height: 45px;
        line-height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-weight: bold;
        font-size: 15px;
        color: #333;
        text-decoration: none;

        img {
          height: 30px;
          position: relative;
          top: 7px;
        }
      }
      a:hover {
        color: #ff8000;
      }
    }

    .sort {
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      // overflow: hidden;

      .all-sort-list2 {
        height: 455px;
        overflow: hidden;
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;

            overflow: hidden;

            margin: 0;

            a {
              height: 100%;
              color: #333;
            }

            // background-color: yellowgreen;
            a:hover {
              color: rgb(201, 66, 66);
              em {
                color: #333;
              }
            }
          }
          h3:hover {
            background-color: rgb(144, 233, 228);
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
        a {
          cursor: pointer;
        }
      }
    }
  }
}
</style>
